import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter)

const routes = [
  // 登录路由
  { path: '/', redirect: '/login' },
  {
    path: '/login',
    name: 'login',
    component: () => import('@/views/user/login.vue'),
    meta: { title: '个人测试-Login' }
  },
  {
    path: '/register',
    name: 'register',
    component: () => import('@/views/user/register.vue'),
    meta: { title: '个人测试-Register' }
  },
  // 首页路由
  {
    path: '/home',
    name: 'home',
    component: () => import('@/views/index.vue'),
    redirect: '/home/shops',
    children: [{ // 商家路由
      path: 'shops',
      name: 'shops',
      component: () => import('@/views/shops/index.vue'),
      meta: { title: 'Shops', show: false }
    },
    {
      path: 'menu',
      name: 'menu',
      component: () => import('@/views/menu/index.vue'),
      redirect: '/menu/goods',
      children: [{ // 菜单路由
        path: 'goods',
        name: 'goods',
        component: () => import('@/views/menu/goods/index.vue'),
        meta: { title: 'Goods', show: true, keepAlive: true }
      },
      {
        path: 'search',
        name: 'search',
        component: () => import('@/views/menu/search/index.vue'),
        meta: { title: 'Search', show: false, keepAlive: true }
      },
      {
        path: 'detail',
        name: 'detail',
        component: () => import('@/views/menu/detail/index.vue'),
        meta: { title: 'Detail', show: false, keepAlive: false }
      },
      {
        path: 'combo',
        name: 'combo',
        component: () => import('@/views/menu/combo/index.vue'),
        meta: { title: 'Combo', show: false, keepAlive: false }
      }]
    },
    {
      path: 'order',
      name: 'order',
      component: () => import('@/views/order/index.vue'),
      redirect: '/order/orders',
      children: [{ // 订单路由
        path: 'orders',
        name: 'orders',
        component: () => import('@/views/order/orders/index.vue'),
        meta: { title: 'Orders', show: true, keepAlive: true }
      },
      {
        path: 'orderdetail',
        name: 'orderdetail',
        component: () => import('@/views/order/detail/index.vue'),
        meta: { title: 'OrderDetail', show: false, keepAlive: false }
      },
      {
        path: 'create-order',
        name: 'create',
        component: () => import('@/views/order/create/index.vue'),
        meta: { title: 'CreateOrder', show: false, keepAlive: false }
      },
      {
        path: 'payments',
        name: 'payments',
        component: () => import('@/views/order/payments/index.vue'),
        meta: { title: 'Payments', show: false, keepAlive: false }
      },
      {
        path: '/ispay',
        name: 'ispay',
        component: () => import('@/views/order/ispay/index.vue'),
        meta: { title: 'PayStatus', show: false, keepAlive: false }
      }]
    },
    {
      path: 'account',
      name: 'account',
      component: () => import('@/views/account/index.vue'),
      redirect: '/home/account/acchome',
      children: [{ // 账户路由
        path: 'acchome',
        name: 'acchome',
        component: () => import('@/views/account/accHome/index.vue'),
        meta: { title: 'Account', show: true }
      },
      {
        path: 'address',
        name: 'address',
        component: () => import('@/views/account/address/index.vue'),
        redirect: '/home/account/address/addlist',
        children: [{ // 地址管理路由
          path: 'addlist',
          name: 'addlist',
          component: () => import('@/views/account/address/addList/index.vue'),
          meta: { title: 'AddList', show: false }
        },
        {
          path: 'addedit',
          name: 'addedit',
          component: () => import('@/views/account/address/addEdit/index.vue'),
          meta: { title: 'AddEdit', show: false }
        }]
      },
      {
        path: 'card',
        name: 'card',
        component: () => import('@/views/account/card/index.vue'),
        redirect: '/home/account/card/cardlist',
        children: [{ // 银行卡管理路由
          path: 'cardlist',
          name: 'cardlist',
          component: () => import('@/views/account/card/cardList/index.vue'),
          meta: { title: 'CardList', show: false }
        },
        {
          path: 'addcard',
          name: 'addcard',
          component: () => import('@/views/account/card/addCard/index.vue'),
          meta: { title: 'AddCard', show: false }
        }]
      },
      {
        path: 'coupon',
        name: 'coupon',
        component: () => import('@/views/account/coupon/index.vue'),
        meta: { title: 'Coupon', show: false }
      },
      {
        path: 'credit',
        name: 'credit',
        component: () => import('@/views/account/credit/index.vue'),
        meta: { title: 'Credit', show: false }
      },
      {
        path: 'favorite',
        name: 'favorite',
        component: () => import('@/views/account/favorite/index.vue'),
        meta: { title: 'Pavorite', show: false }
      },
      {
        path: 'point',
        name: 'point',
        component: () => import('@/views/account/point/index.vue'),
        meta: { title: 'Point', show: false }
      },
      {
        path: 'settings',
        name: 'settings',
        component: () => import('@/views/account/settings/index.vue'),
        meta: { title: 'Settings', show: false }
      }]
    },
    { // 首页定位路由
      path: 'position',
      name: 'position',
      component: () => import('@/views/shops/position/index.vue'),
      meta: { title: 'Position', show: false }
    }]
  }
]

const router = new VueRouter({
  routes
})

router.beforeEach(function (to, from, next) {
  document.title = to.meta.title
  const token = localStorage.getItem('assets_token_order')
  if (to.path !== '/login' && to.path !== '/register') {
    if (token) {
      next()
    } else {
      next('/login')
    }
  } else {
    next()
  }
})

export default router
